import { getArticleDetail,changelikes,deatilread } from '@/services';
import { listItem } from '@/utils/Article';
// import {IknowledgeListItem} from '@/utils/interface'
import { Effect,ImmerReducer, Reducer, Subscription} from 'umi';
export interface IndexModelState {
    detailList:Array<listItem>,
    readDetailList:listItem
    detailReadList:listItem
  }
export interface IndexModelType {
    namespace: 'Detail';
    state: IndexModelState;
    effects: {
        getArticleDetail:Effect,
        changelikes:Effect,
        deatilread:Effect
    };
    reducers: {
      save: Reducer<IndexModelState>;
      // 启用 immer 之后
      // save: ImmerReducer<IndexModelState>;
    };
    // subscriptions: { setup: Subscription };
  }
  
  const IndexModel: IndexModelType = {
    namespace: 'Detail',
    state: {
      detailList: [],
      // readDetailList:{},
      // detailReadList:{}
    },
    effects: {
      *getArticleDetail({ payload }, { call, put }) {
        let res = yield call(getArticleDetail, payload.id);
        yield put({
          type: 'save',
          payload: {
            detailList: res.data
          }
        });
      },
       //喜欢
    *changelikes({payload},{call,put}){
      let par=payload.like?"dislike":"like"
      let type={type:par}
      let result=yield call(changelikes,payload.id,type);
      yield put({
        type:"save",
        payload:{
          readDetailList:result.data
        }
      })
    },
    //详情阅读
    *deatilread({payload},{call,put}){
      let result =yield call(deatilread,payload.id);
      yield put({
        type:"save",
        payload:{
          detailReadList:result.data
        }
      })
    },
    },
    reducers: {
      save(state, action) {
        return {
          ...state,
          ...action.payload,
        };
      }
      // 启用 immer 之后
      // save(state, action) {
      //   state.name = action.payload;
      // },
    },
  };
  
  export default IndexModel;